#!/usr/bin/ksh
######################################################################
# $Id:$
# $Url:$
# $Revision: $
# $Author: $
# $Date: $
######################################################################

FS_LIST="$*"

set -o nounset

 INFO=" INFO:"
ERROR="ERROR:"
 WARN=" WARN:"
BLANK="      " 
 NOTE=" NOTE:" 

FILE1=$( mktemp )
FILE2=$( mktemp )

format </dev/null | 
   awk '/cyl/ { ctd=$2; getline; print ctd, $1 }' | sed -e "s/sd/disk/" > $FILE1

function hwpath_to_ctd
{
   typeset HWPATH=$1
   
   grep " ${HWPATH}$" $FILE1 | awk '{ print $1 }'
}

function ctd_to_hwpath
{
   typeset CTD=$1
   
   grep "^${CTD} " $FILE1 | awk '{ print $2 }'
}

# Cual es el ctd del disco de boot
# por ej: c1t0d0, /pci@1c,600000/scsi@2/disk@0,0

HWPATH_BOOT=$( prtconf -pvPD | grep bootpath | awk -F"'" '{ print $2 }' | awk -F: '{ print $1 }' )   

CTD_BOOT=$( hwpath_to_ctd ${HWPATH_BOOT} )


FS_LIST=$( for FS in / $FS_LIST
do
   echo $FS
done | sort -u )

for FS in $FS_LIST
do

   MD=$( df -k $FS | tail -1 | cut -d" "  -f1 | cut -d/ -f5 )
   

   echo
   echo "$INFO" "$FS\t-->" ${MD}
   echo
   metastat -p ${MD}
   
   if [ $(metastat -p ${MD} | wc -l ) -ne 3 ]
   then
      echo >&2 $ERROR "El / no esta en mirror"
      # exit 1
   fi
   
   if [ "$FS" = "/" ]
   then
      MD_ROOT=$MD
      
      # Chequeamos que el disco de boot sea una pata del mirror del raiz
      if metastat -p ${MD_ROOT} | grep $CTD_BOOT >/dev/null 2>&1
      then
         : # ok, seguimos con el script
      else
         echo >&2 "$ERROR" "El disco de boot no es una pata del mirror del raiz:"
         exit 1
      fi

   fi
      
done
   
# De los dos discos del mirror del /, el que no es el de boot es el alternate:
CTD_ALT=$( metastat -p ${MD_ROOT} | egrep -e "c.*t.*d.*" | cut -d" " -f4 | cut -d"s" -f1 | grep -v $CTD_BOOT )
HWPATH_ALT=$( ctd_to_hwpath ${CTD_ALT} )

echo      
echo "$INFO" "Orden arranque *por defecto* en OBP:"
# prtconf -pvPD | grep "boot-device:"
/usr/sbin/eeprom boot-device

echo      
echo "$INFO" $( eeprom auto-boot? )

# Aliases OBP (devalias)
prtconf -pvPD | grep "'/" |
      grep -v "bootpath" | grep -v "boot-device" |
      grep "disk.*:" | sed -e "s#[ ]*##g; s/\'//g; s/:/ /" > $FILE2
      
function hwpath_to_obp
{

   typeset HWPATH=$1
   
   grep " ${HWPATH}$" $FILE2 | awk '{ print $1 }' | head -1

}

OBP_ALIAS_PRI=$( hwpath_to_obp $HWPATH_BOOT )
OBP_ALIAS_ALT=$( hwpath_to_obp $HWPATH_ALT )

echo
echo "$INFO" Discos de boot: 
echo
echo PRI: ${OBP_ALIAS_PRI}, ${CTD_BOOT}, ${HWPATH_BOOT}
echo ALT: ${OBP_ALIAS_ALT}, ${CTD_ALT}, ${HWPATH_ALT}

echo
echo "$NOTE" "Para botar del disco ACTIVO   : {0} ok boot" ${OBP_ALIAS_PRI}
echo "$NOTE" "Para botar del disco SPLITEADO: {0} ok boot" ${OBP_ALIAS_ALT}

# Generamos una tabla con FS, MD principal, MD a desatachar, Slice (ctd) spliteada

TABLA=""

for FS in ${FS_LIST}
do
   
   MD1=$( df -k $FS | tail -1 | cut -d" "  -f1 | cut -d/ -f5 )
   MD2=$(   metastat -p $MD1 | grep $CTD_ALT | awk '{ print  $1 }' )
   SLICE=$( metastat -p $MD1 | grep $CTD_ALT | awk '{ print $NF }' )
   
   TABLA="$TABLA
   $FS $MD1 $MD2 $SLICE"

done

TABLA=$( echo "$TABLA" | sort -r | grep -v "^$" )

echo
echo "$NOTE Para splitear el SO, ejecutar lo siguiente:"
echo "$WARN" "Splitear el / el ultimo"
echo

echo "$TABLA" | while read FS MD1 MD2 SLICE
do
   echo "$BLANK #" "metadetach $MD1 $MD2; fsck -y /dev/rdsk/${SLICE}"
done

# aqui $SLICE es la slice del raiz spliteado:
echo
[ -d /a ] || echo "$BLANK #" mkdir /a
echo "$BLANK #" mount /dev/dsk/$SLICE /a

echo "$BLANK #" vi /a/etc/vfstab

echo
echo "# Comentar las siguientes lineas:"
echo

echo "$TABLA" | sort | while read FS MD1 MD2 SLICE
do
   grep "md/dsk/$MD1[ 	]" /etc/vfstab
done

echo
echo "# Poner estas lineas:"
echo

echo "$TABLA" | sort | while read FS MD1 MD2 SLICE
do
   echo "/dev/dsk/$SLICE	/dev/rdsk/$SLICE	$FS	ufs 1 no -"
done

echo
echo "$BLANK #" vi /a/etc/system

echo
echo "# Comentar con * las siguientes lineas:"
grep rootdev /etc/system | grep pseudo
grep mddb_boot /etc/system

echo
echo "$BLANK #" umount /a
echo

rm $FILE1 $FILE2
